//
//  FctView.m
//  CarHome
//
//  Created by 张世琦 on 14-9-23.
//  Copyright (c) 2014年 WJN_work@163.com. All rights reserved.
//

#import "FctView.h"
#import "FctCarModel.h"
#import "FctModel.h"
#import "SpecView.h"
#import "DeliverModel.h"

NSString *const fctCell = @"cellId";//重用池

@implementation FctView

- (void)dealloc
{
    [_letterArray release];
    _letterArray = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _letterArray = [[NSMutableArray alloc] init];
        [self createTableView];
    }
    return self;
}

#pragma mark -
#pragma mark 创建TableView

- (void)createTableView
{
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, self.frame.size.height - 20)];
    aView .backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, 44)];
    bView.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 44)];
    label.text = @"选择车型";
    [bView addSubview:label];
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(190, 0, 50, 44)];
    [rightButton setTitle:@"关闭" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bView addSubview:rightButton];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(0, 0, 50, 44)];
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [bView addSubview:leftButton];
    [aView addSubview:bView];
    UIButton *all = [UIButton buttonWithType:UIButtonTypeCustom];
    [all setFrame:CGRectMake(0, 0, 320, 40)];
    [all setTitle:@"全部车系" forState:UIControlStateNormal];
    [all setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [all addTarget:self action:@selector(allAction:) forControlEvents:UIControlEventTouchUpInside];
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 74, 240, self.frame.size.height - 69 - 188) style:UITableViewStylePlain];
    _table.tableHeaderView = all;
    [_table registerClass:[UITableViewCell class] forCellReuseIdentifier:fctCell];
    _table.dataSource = self;
    _table.delegate = self;
    [aView addSubview:_table];
    [self addSubview:aView];
}
//点击事件
- (void)allAction:(id)sender
{
    DeliverModel *model = [[DeliverModel alloc] init];
    model.fctId = self.fctId;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"chexi" object:model];
    [self.superview removeFromSuperview];
}

#pragma mark - 
#pragma mark 关闭, 返回按钮点击事件

- (void)buttonAction:(id)sender
{
    [self.superview removeFromSuperview];
}

- (void)button:(id)sender
{
    [self removeFromSuperview];
}

#pragma mark -
#pragma mark UITableViewDataSource协议
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    FctCarModel *fctCar = [_letterArray objectAtIndex:section];
    return [fctCar.list count];
}
//重用机制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:fctCell forIndexPath:indexPath];
    FctCarModel *fctCar = [_letterArray objectAtIndex:indexPath.section];
    FctModel *fct = [fctCar.list objectAtIndex:indexPath.row];
    [cell.textLabel setText:fct.name];
    return cell;
}

//section数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_letterArray count];
}

//section标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    FctCarModel *fctCar = [_letterArray objectAtIndex:section];
    return fctCar.letter;
}

#pragma mark -
#pragma mark UITableViewDelegate协议
//cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    FctCarModel *fctCar = [_letterArray objectAtIndex:indexPath.section];
    FctModel *fct = [fctCar.list objectAtIndex:indexPath.row];
    SpecView *aView = [[SpecView alloc] initWithFrame:CGRectMake(0, 0, 240, self.frame.size.height + 64 + 49)];
    aView.fctId = self.fctId;
    aView.specId = fct.carId;
    [aView conncetion:[NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.2/cars/specslist-a2-pm1-v4.2.0-t0x000c-ss%ld.html", (long)fct.carId]];
    [self addSubview:aView];
    [aView release];
}

#pragma mark -
#pragma mark 网络请求

- (void)conncetion:(NSString *)url
{
    [NetUrl connect:url parmater:nil Block:^(NSData *data) {
        NSMutableDictionary *dic = (NSMutableDictionary *)data;
        NSMutableDictionary *result = [dic objectForKey:@"result"];
        NSMutableArray *fctlist = [result objectForKey:@"fctlist"];
        for (NSMutableDictionary *dic in fctlist) {
            FctCarModel *fctCar = [[FctCarModel alloc] init];
            fctCar.letter = [dic objectForKey:@"name"];
            NSMutableArray *serieslist = [dic objectForKey:@"serieslist"];
            for (NSMutableDictionary *dic in serieslist) {
                FctModel *fct = [[FctModel alloc] init];
                fct.name = [dic objectForKey:@"name"];
                fct.carId = [[dic objectForKey:@"id"]intValue];
                [fctCar.list addObject:fct];
            }
            [_letterArray addObject:fctCar];
        }
        [_table reloadData];
    }];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
